package br.faimg.pomar.controle.componentevisual;


import br.faimg.pomar.controle.BusinessDelegate.RotaEstacaoBusinessDelegate;
import br.faimg.pomar.modelo.dao.RotaEstacaoDAO;
import br.faimg.pomar.modelo.pojo.RotaEstacao;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.swing.tree.MutableTreeNode;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author flavio
 */
public class RotaEstacaoGraphModel {
    
    //Nao esquecer de pegar a rota solicitada pelo 
    public GraphModel getModel(Integer ordemProducao){
        
        GraphModel model = new DefaultGraphModel();
        GraphLayoutCache view = new GraphLayoutCache(model, new DefaultCellViewFactory());

        JGraph graph = new JGraph(model, view);

        List<DefaultGraphCell> cells = new ArrayList<DefaultGraphCell>();

        RotaEstacaoBusinessDelegate delegate = new RotaEstacaoBusinessDelegate();
        List<RotaEstacao> rotaEstacao = delegate.readByExample("", "", "", ordemProducao);
        

        //Determina quais as estacoes da rota
        int posx = 20;
        int posy = 50;
        int sequenciaAnterior = -1;
        
        List<Integer> posxList = new ArrayList<Integer>();
        
        for(int i =0; i < rotaEstacao.size()-1; i++){
            
            cells.add(new DefaultGraphCell(new String(
                    rotaEstacao.get(i).getEstacaoProxima().getCodigo())));
            
            //Se for igual é porque é uma rota alternativa
            if (sequenciaAnterior == rotaEstacao.get(i).getSquencia().intValue()){
                
                posy = 150;
                
            }else{
                
                sequenciaAnterior = rotaEstacao.get(i).getSquencia().intValue();
                
            }
            
            //Armazena as estacoes de 
            if (rotaEstacao.get(i).getEstacaoProxima().getTipo().getCodigo().equals("TET   ")){
            
                posxList.add(i);
                
            }
            
            //Adiciona um novo port para conexao de um novo edge, pois esta estacão terá duas setas.
            if (rotaEstacao.get(i).getEstacaoProxima().getTipo().getCodigo().equals("TET   ")
                    || rotaEstacao.get(i).getEstacaoProxima().getTipo().getCodigo().equals("TER   ")){
                
                DefaultPort port = new DefaultPort();
                cells.get(i).add(port);
            }
                
            
            GraphConstants.setBounds(cells.get(i).getAttributes(), new Rectangle2D.Double(posx, posy, 90, 20));
                        
            GraphConstants.setGradientColor(cells.get(i).getAttributes(), Color.orange);

            GraphConstants.setOpaque(cells.get(i).getAttributes(), true);

            DefaultPort port = new DefaultPort();
            
            cells.get(i).add(port);
            
            //Altera a posicao x e y das estacoes
            posx = posx+150;
            posy = 50;
            
        }
        
        
        int arrow = GraphConstants.ARROW_CLASSIC;
        int cellsSize = cells.size();
        for (int i = 1; i < cellsSize; i++) {

            //liga as estacoes que nao sao de reparo
            if (!rotaEstacao.get(i).getEstacaoProxima().getTipo().getCodigo().equals("TER   ") &&
                    rotaEstacao.get(i).getSquencia().equals(i)) {
                DefaultEdge edge = new DefaultEdge();

                edge.setSource(cells.get(i - 1).getChildAt(0));

                edge.setTarget(cells.get(i).getChildAt(0));

                GraphConstants.setLineEnd(edge.getAttributes(), arrow);

                GraphConstants.setEndFill(edge.getAttributes(), true);

                cells.add(edge);
        
            } else {
                //Liga as estacoes de reparo as estacoes de teste e vice versa.
                int ii = posxList.remove(0).intValue();

                DefaultEdge edge = new DefaultEdge();

                edge.setSource(cells.get(ii).getChildAt(0));

                edge.setTarget(cells.get(i).getChildAt(0));
                
                GraphConstants.setLineEnd(edge.getAttributes(), arrow);

                GraphConstants.setEndFill(edge.getAttributes(), true);
                
                cells.add(edge);
                
                
                
                //Seta de retorno
                DefaultEdge edge1 = new DefaultEdge();
                
                edge1.setSource(cells.get(i).getChildAt(1));
                
                edge1.setTarget(cells.get(ii).getChildAt(1));

                GraphConstants.setLineEnd(edge1.getAttributes(), arrow);

                GraphConstants.setEndFill(edge1.getAttributes(), true);

                cells.add(edge1);
            }

        }
        
        graph.getGraphLayoutCache().insert(cells.toArray());
        
        return graph.getModel();
    }
    

}
